package com.zhang.crowd.service.impl;

import com.zhang.crowd.entity.po.MemberPO;
import com.zhang.crowd.entity.po.MemberPOExample;
import com.zhang.crowd.mapper.MemberPOMapper;
import com.zhang.crowd.service.api.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//在类上使用Transactional(readOnly = true)针对查询操作设置事务属性
@Transactional(readOnly = true)
@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberPOMapper memberPOMapper;

    @Override
    public MemberPO getMemberPOByLoginAcct(String loginacct) {
        //创建Example对象
        MemberPOExample memberPOExample = new MemberPOExample();
        //创建Criteria对象
        MemberPOExample.Criteria criteria = memberPOExample.createCriteria();
        //封装查询条件
        criteria.andLoginacctEqualTo(loginacct);
        //执行查询
//        MemberPO memberPO = memberPOMapper.selectByExample(memberPOExample).get(0);
        List<MemberPO> list = memberPOMapper.selectByExample(memberPOExample);
        if(list == null || list.size() == 0){
            return null;
        }

        return list.get(0);
    }
    @Transactional(
            propagation = Propagation.REQUIRES_NEW,
            rollbackFor = Exception.class,
            readOnly = false
    )
    @Override
    public void saveMember(MemberPO memberPO) {
        memberPOMapper.insertSelective(memberPO);
    }
}
